package com.web.exiaodao.business.service.impl;

import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.result.XaResult;
import com.web.webstart.base.util.XaUtil;
import com.web.exiaodao.business.entity.TradeRecord;
import com.web.exiaodao.business.repository.TradeRecordRepository;
import com.web.exiaodao.business.service.TradeRecordService;

@Service("TradeRecordService")
@Transactional(readOnly = false)
public class TradeRecordServiceImpl extends BaseService<TradeRecord> implements TradeRecordService {

	private static final Logger LOGGER = Logger.getLogger(TradeRecordServiceImpl.class);

	@Autowired
	private TradeRecordRepository tradeRecordRepository;
	
	/**
	 * 查询单条TradeRecord信息
	 * //@param tId
	 * @return 返回单个TradeRecord对象
	 * @throws BusinessException
	 */
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public XaResult<TradeRecord> findOne(Long modelId) throws BusinessException {
		TradeRecord obj = new TradeRecord();
		if(modelId != 0){
			obj = tradeRecordRepository.findByIdAndStatusNot(modelId,XaConstant.Status.delete);
		}
		XaResult<TradeRecord> xr = new XaResult<TradeRecord>();
		if (XaUtil.isNotEmpty(obj)) {
			xr.setObject(obj);
			LOGGER.info("后台----->查询单个实体----->TradeRecord");
		} else {
			xr.error(XaConstant.Message.object_not_find);
			LOGGER.info(XaConstant.Message.object_not_find);
		}
		return xr;
	}

	/**
	 * 分页查询状态非status的TradeRecord数据
	 * @param status
	 * @param filterParams
	 * @param pageable
	 * @return 返回对象TradeRecord集合
	 * @throws BusinessException
	 */
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public XaResult<Page<TradeRecord>> findListNEStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable) throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示非删除的所有数据
			status = XaConstant.Status.delete;
		}
		filters.put("status", new SearchFilter("status", Operator.NE, status));
		Page<TradeRecord> page = tradeRecordRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), TradeRecord.class), pageable);
		XaResult<Page<TradeRecord>> xr = new XaResult<Page<TradeRecord>>();
		xr.setObject(page);
		LOGGER.info("后台----->分页查询实体集合（状态不等于）----->TradeRecord");
		return xr;
	}

	/**
	 * 分页查询状态status的TradeRecord数据
	 * @param status
	 * @param filterParams
	 * @param pageable
	 * @return 返回对象TradeRecord集合
	 * @throws BusinessException
	 */
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public XaResult<Page<TradeRecord>> findListEQStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable) throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示正常数据
			status = XaConstant.Status.valid;
		}
		filters.put("status", new SearchFilter("status", Operator.EQ, status));
		Page<TradeRecord> page = tradeRecordRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), TradeRecord.class), pageable);
		XaResult<Page<TradeRecord>> xr = new XaResult<Page<TradeRecord>>();
		xr.setObject(page);
		LOGGER.info("后台----->分页查询实体集合（状态等于）----->TradeRecord");
		return xr;
	}

	/**
	 * 保存TradeRecord信息
	 * @param model
	 * @return
	 * @throws BusinessException
	 */
	@Transactional(rollbackFor = Exception.class)
	public XaResult<TradeRecord> saveOrUpdate(TradeRecord model) throws BusinessException {
		TradeRecord obj = null;
		if(XaUtil.isNotEmpty(model.getId())){
			obj = tradeRecordRepository.findOne(model.getId());
		}else{
			obj = new TradeRecord();
		}
	obj.setCreateUser(XaUtil.isNotEmpty(model.getCreateUser()) ? model.getCreateUser() : obj.getCreateUser());
	    obj.setUserId(XaUtil.isNotEmpty(model.getUserId()) ? model.getUserId() : obj.getUserId());
	    obj.setMoney(XaUtil.isNotEmpty(model.getMoney()) ? model.getMoney() : obj.getMoney());
	    obj.setType(XaUtil.isNotEmpty(model.getType()) ? model.getType() : obj.getType());
	    obj.setOrderNo(XaUtil.isNotEmpty(model.getOrderNo()) ? model.getOrderNo() : obj.getOrderNo());
		obj = tradeRecordRepository.save(obj);
		XaResult<TradeRecord> xr = new XaResult<TradeRecord>();
		xr.setObject(obj);
		LOGGER.info("后台新增或编辑数据成功！");
		return xr;
	}

	/**
	 * 修改TradeRecord状态，可一次修改多条   3删除  -1锁定  1正常
	 * //@param userId
	 * @param modelIds
	 * @param status
	 * @return 返回TradeRecord对象
	 * @throws BusinessException
	 */
	@Transactional(rollbackFor = Exception.class)
	public XaResult<TradeRecord> multiOperate(
			String modelIds,Integer status) throws BusinessException {
		XaResult<TradeRecord> xr = new XaResult<TradeRecord>();
		if(status == null){
			status = XaConstant.Status.delete;
		}
		if(modelIds != null){
			String[] ids = modelIds.split(",");
			for(String id : ids){
				TradeRecord obj = tradeRecordRepository.findByIdAndStatusNot(Long.parseLong(id),status);
				if (XaUtil.isNotEmpty(obj)) {
					obj.setStatus(status);
					obj = tradeRecordRepository.save(obj);
				}
			}
		}
		LOGGER.info("批量修改数据状态成功！");
		return xr;
	}
	
}
